package com.concerneyes;

import java.util.ArrayList;
import java.util.List;

import com.bumptech.glide.Glide;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

import common.BaseActivity;
import common.PreferUtil;


/**
 * Depiction: 新手引导界面
 * <p>
 * Modify:
 * <p>
 * Author: Kevin Lynn
 * <p>`
 * Create Date：2016年1月25日 下午9:43:17
 * <p>
 * 
 * @version 1.0
 * @since 1.0
 */
public class GuideActivity extends BaseActivity {
	private ViewPager	 viewPager;
	private ImageAdapter adapter;
	private boolean		 from_splash;
	
	@Override
	protected void onCreate(Bundle bundle) {
		super.onCreate(bundle);
		setContentView(R.layout.activity_guide);
		
		from_splash = getIntent().getBooleanExtra("from_splash", false);
		
		viewPager = (ViewPager) findViewById(R.id.viewpager);
		adapter = new ImageAdapter(getApplicationContext());
		viewPager.setAdapter(adapter);
		
	}
	
	private class ImageAdapter extends PagerAdapter {
		private int[]			icons = {
		                                  R.drawable.a,
		                                  R.drawable.b,
		                                  R.drawable.c,
		};
		
		private List<ImageView>	views;
		
		public ImageAdapter(Context context) {
			views = new ArrayList<ImageView>();
			for (int i = 0, size = icons != null ? icons.length : 0; i < size; i++) {
				ImageView iv = new ImageView(context);
				iv.setScaleType(ScaleType.FIT_XY);
				LayoutParams params = new LayoutParams(-1, -1);
				iv.setLayoutParams(params);
				
				Glide.with(getApplicationContext()).load(icons[i]).placeholder(R.drawable.a).into(iv);
				views.add(iv);
				
				if (i == icons.length - 1) {
					iv.setOnClickListener(new OnClickListener() {
						@Override
						public void onClick(View v) {
							if (from_splash) {
								openActivity(SplashActivity.class, null);
							}
							PreferUtil.saveVersion(getVersionName());
							finish();
						}
					});
				}
			}
		}
		
		@Override
		public int getCount() {
			return icons != null ? icons.length : 0;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView(views.get(position));
		}
		
		@Override
		public int getItemPosition(Object object) {
			return super.getItemPosition(object);
		}
		
		@Override
		public ImageView instantiateItem(ViewGroup container, int position) {
			container.addView(views.get(position));
			return views.get(position);
		}
		
		@Override
		public boolean isViewFromObject(View view1, Object view2) {
			return view1 == view2;
		}
	}
}
